<?php
  include_once $_SERVER['DOCUMENT_ROOT'].'/PariGest/pages/inc/entete.php';
  require_once $REP_CLASS . 'class.Operation.php';
  require_once $REP_CLASS . 'class.Parametrage.php';
  
  include $REP_PAGES . 'IncSelectCompte.php';
  
  try
  {
    /*$parametrage = new Parametrage($db);
    
    $parametrage->setIdCompte($IdCompte);
    $parametrage->affichage();*/
    
   if( $action == 'UPDATE' )
   {
     $operation = new Operation($db);
     $operation->setIdType(getChaineNotNullSinon("LstTypeOp",""));
     $operation->setDate(convMajDate(getChaineNotNullSinon("Date","")));
     $operation->setCommentaire(getChaineNotNullSinon("Commentaire",""));
     $operation->setIdCompte($IdCompte);
     $operation->setLibelle(getChaineNotNullSinon("Libelle",""));
     $operation->setMontant(getChaineNotNullSinon("Montant",""));
     if( $operation->getIdType() == Operation::$ID_TYPE_RETRAIT )
     {
       $operation->setMontant("-" . $operation->getMontant());
     }
     $operation->insertion();
   }

    $trace->fb('Avant le commit');
    $db->commit();
    $trace->fb('Apres le commit');
    
    $lstTypeOp = Operation::selectAllTypes();
  }
  catch(MyException $e)
  {
    $trace->fb('!!!!! ERREUR !!!!!');
    include $_SERVER['DOCUMENT_ROOT'].'/PariGest/pages/inc/catch.php';
  }
  $nbEnregParPage = 10;
    
  $operation = new Operation($db);
  $operation->setIdCompte($IdCompte);
  
  $nbEnreg =  $operation->select(Selector::$SENS_TRI_DESC)->rowCount();
  
  $nbPages = ceil($nbEnreg / $nbEnregParPage);
  
  $numPage = getChaineNotNullSinon("NumPage","1");
  if( getChaineNotNullSinon("ActionMultiPage","") == "FIRST" )
  {
    $numPage = 1;
  }
  else if( getChaineNotNullSinon("ActionMultiPage","") == "LAST" )
  {
    $numPage = $nbPages;
  }
  else if( getChaineNotNullSinon("ActionMultiPage","") == "NEXT" )
  {
    $numPage += 1;
  }
  else if( getChaineNotNullSinon("ActionMultiPage","") == "PREV" )
  {
    $numPage -= 1;
  }
  
  $iDebut = ($numPage-1) * $nbEnregParPage;
  $stmt =  $operation->selectLimit(Selector::$SENS_TRI_DESC, $iDebut, $nbEnregParPage);
  $result = $stmt->fetchAll();
  
  $smarty->assign('REP_PAGES', $REP_PAGES);
  $smarty->assign('LstTypeOp', $lstTypeOp);
  $smarty->assign('DateDuJour', getDateToday());
  $smarty->assign('LstTypeOpSelected', Operation::$ID_TYPE_DEPOT);
  $smarty->assign('LstOperations', $result);
  
  $smarty->assign('NumPage', $numPage);
  $smarty->assign('NbPages', $nbPages);
  $smarty->assign('NbEnregParPage', $nbEnregParPage);
  
  $smarty->display('operation_compte.tpl');
?>
